package com.yenlien.traceyun.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.bean.RestURL;
import com.yenlien.traceyun.business.dao.WexinSubDao;
import com.yenlien.traceyun.business.entity.Subpackage;
import com.yenlien.traceyun.business.entity.WexinSub;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.PlantSubPackService;
import com.yenlien.traceyun.business.service.SubpackageService;
import com.yenlien.traceyun.business.service.WexinSubService;
import com.yenlien.traceyun.business.service.rest.RestService;
import com.yenlien.traceyun.common.utils.ImgUtil;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 分包列表
 *
 * @author maybe
 * @date 2020年10月22日
 */
@RestController
@RequestMapping("/plantsub")
public class SubpackageController extends BaseController {
    final static String imgName2 = "gou.png";        // 有勾：

    final static String imgName1 = "nogou.png";        //没有勾：
    @Autowired
    private SubpackageService subpackageService;

    @Autowired
    private PlantSubPackService plantSubPackService;

    @Autowired
    private WexinSubService wexinSubService;

    @Autowired
    private WexinSubDao wexinSubDao;

    @Autowired
    private RestService restService;


    //分包
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody Subpackage subpackage) {
        JSONObject param = new JSONObject();
        param.put("companyId", UserAuthUtil.getLoginUser().getCompanyId());

        JSONObject maxPrintAndmaxScan = getMaxPrintAndmaxScan();        //控制中心符码数量的比较
        int maxPrintOld = maxPrintAndmaxScan.getIntValue("max_print");        //控制中心当前剩余分包总数
        int branchPackageNum = subpackage.getBranchPackageNum();        //当前分包总数
//		if(maxPrintOld>branchPackageNum){
//			int maxPrintNew = maxPrintOld - branchPackageNum;
//			param.put("maxPrint",maxPrintNew);
//		}else{
//			throw new BusinessException("500","追溯码绑定的数量不能大于控制中心配置的最大符码量，请联系管理员");
//		}
//		updateMaxPrintAndMaxScan(param);		//更新控制中心符码数量
        plantSubPackService.save(subpackage);
        return success("success");
    }


    //分包
    @RequestMapping(value = "/wexin/save", method = RequestMethod.POST)
    public Object wexinSub(@RequestBody JSONObject params) {
        wexinSubService.wexinSub(params);
        return success("success");
    }

    @GetMapping(value = "/loadwexin/{subId}")
    public Object loadWexin(@PathVariable("subId") String subId) {
        WexinSub bySubId = wexinSubDao.findBySubId(subId);

        String usedDrug16 = bySubId.isUsedDrug() ? ImgUtil.platePhotoURL(imgName2) : ImgUtil.platePhotoURL(imgName1);
        bySubId.setUsedDrug16(usedDrug16);

        String isUsedAdd16 = bySubId.isUsedAdd() ? ImgUtil.platePhotoURL(imgName2) : ImgUtil.platePhotoURL(imgName1);
        bySubId.setUsedAdd16(isUsedAdd16);

        String observeRule16 = bySubId.isObserveRule() ? ImgUtil.platePhotoURL(imgName2) : ImgUtil.platePhotoURL(imgName1);
        bySubId.setObserveRule16(observeRule16);

        String observeSale16 = bySubId.isObserveSale() ? ImgUtil.platePhotoURL(imgName2) : ImgUtil.platePhotoURL(imgName1);
        bySubId.setObserveSale16(observeSale16);
        return success(bySubId);
    }

    /**
     * 获取数据中心配置的最大扫码量和符码量
     */
    public JSONObject getMaxPrintAndmaxScan() {
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        JSONObject json = restService.getJson(RestURL.USERCENTER + "company/findByCompanyIdMaxScan/" + companyId);
        if (json == null) {
            throw new BusinessException("500", "未查询到控制中心配置的最大扫码量跟最大符码量，联系管理员配置");
        }
        return json.getJSONObject("map");
    }

    /**
     * 更新控制中心的最大符码跟扫码量
     */
    public JSONObject updateMaxPrintAndMaxScan(JSONObject param) {
        JSONObject json = restService.postJson(RestURL.USERCENTER + "company/updateMaxPrintAndMaxScan", param);
        return json;
    }

}
